package com.ruoyi.framework.listener;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.SysConfigConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;

/**
 * @Classname SetUpWebDataInitListener
 * @Author: sunluquan
 * @Date: 2022/2/28 10:19
 * @Description TODO
 * 设置一些全局的web数据
 */
@Component
public class SetUpWebDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

    //是否忽略页脚
    private final static String IGNORE_FOOTER="ignoreFooter";

    @Autowired
    private ISysConfigService sysConfigService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {


        if(event.getApplicationContext() instanceof WebApplicationContext){
            WebApplicationContext applicationContext = (WebApplicationContext)event.getApplicationContext();
            ServletContext servletContext = applicationContext.getServletContext();

            if(StringUtils.isNull(servletContext.getAttribute(IGNORE_FOOTER))){

                servletContext.setAttribute(IGNORE_FOOTER,sysConfigService.selectConfigByKey(SysConfigConstants.IS_IGNORE_FOOTER_KEY));

                servletContext.setAttribute("copyrightYear", RuoYiConfig.getCopyrightYear());

            }
        }
    }
}
